import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layout'

Vue.use(VueRouter)

export const routes = [
  // 前台路由
  {
    path: '/',
    component: () => import('@/views/dashboard/index'),
    name: 'Home'
  },
  // 前台自习室列表
  {
    path: '/rooms',
    component: () => import('@/views/study-room/list/front'),
    name: 'Rooms',
    meta: { 
      title: '自习室',
      activeMenu: '/rooms'
    }
  },
  {
    path: '/room/detail/:id',
    component: () => import('@/views/study-room/detail/index'),
    name: 'StudyRoomDetail',
    meta: { title: '自习室详情' }
  },
  {
    path: '/study-room/order',
    name: 'StudyRoomOrder',
    component: () => import('@/views/study-room/order/index.vue'),
    meta: { title: '座位预约' }
  },
  {
    path: '/lost-found',
    component: () => import('@/views/lost-found/index'),
    name: 'LostFound'
  },
  {
    path: '/forum',
    component: () => import('@/views/forum/index.vue'),
    name: 'Forum'
  },
  {
    path: '/forum/post/:id',
    name: 'PostDetail',
    component: () => import('@/views/forum/detail.vue'),
    props: true,
    meta: { 
      title: '帖子详情',
      keepAlive: false
    }
  },
  // 登录页
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  // 后台管理路由
  {
    path: '/admin',
    component: Layout,
    redirect: '/admin/system/user',
    children: [
      // 系统管理
      {
        path: 'system/user',
        component: () => import('@/views/system/user/index'),
        name: 'User',
        meta: { title: '用户管理' }
      },
      {
        path: 'system/role',
        component: () => import('@/views/system/role/index'),
        name: 'Role',
        meta: { title: '角色管理' }
      },
      {
        path: 'system/permission',
        component: () => import('@/views/system/permission/index'),
        name: 'Permission',
        meta: { title: '权限管理' }
      },
      // 自习室管理
      {
        path: 'study-room/list',
        component: () => import('@/views/study-room/list'),
        name: 'RoomList',
        meta: { title: '房间列表' }
      },
      {
        path: 'study-room/booking',
        component: () => import('@/views/study-room/booking'),
        name: 'Booking',
        meta: { title: '预约管理' }
      },
      {
        path: 'study-room/rules',
        component: () => import('@/views/study-room/rules'),
        name: 'BookingRules',
        meta: { title: '预约规则管理' }
      },
      {
        path: 'info/notice',
        component: () => import('@/views/info/notice/index'),
        name: 'Notice',
        meta: { title: '公告管理' }
      },
      // 我的预约
      {
        path: 'study-room/myOrder',
        component: () => import('@/views/study-room/myOrder'),
        name: 'MyOrder',
        meta: { title: '我的预约' }
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  // 判断是否是后台路由
  const isAdminRoute = to.path.startsWith('/admin')
  
  if (to.path === '/login') {
    if (token) {
      // 已登录时访问登录页，重定向到后台首页
      next('/admin/system/user')
    } else {
      next()
    }
  } else if (isAdminRoute) {
    // 后台路由需要登录验证
    if (!token) {
      next('/login')
    } else {
      next()
    }
  } else {
    // 前台路由直接放行
    next()
  }

  // 如果是从论坛列表页到详情页
  if (to.name === 'PostDetail' && from.name === 'Forum') {
    // 确保组件重新渲染
    to.meta.keepAlive = false
  }
})

export default router 